/******************************************************************************
 *                  Shanghai ChipON Micro-Electronic Co.,Ltd                  *
 ******************************************************************************
 *  $File Name$       : main.c                                         	      *
 *  $Author$          : ChipON AE/FAE Group                                   *
 *  $Data$            : 2021-07-8                                             *
 *  $Hard Version     : KF32A156-MINI-EVB_V1.2                                *
 *  $Description$     : This file provides a routine for CCP capture mode     *
 ******************************************************************************
 *  Copyright (C) by Shanghai ChipON Micro-Electronic Co.,Ltd                 *
 *  All rights reserved.                                                      *
 *                                                                            *
 *  This software is copyright protected and proprietary to                    *
 *  Shanghai ChipON Micro-Electronic Co.,Ltd.                                 *
 ******************************************************************************
 *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  *
 *                     		REVISON HISTORY                               	  *
 *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  *
 *  Data       Version  Author        Description                             *
 *  ~~~~~~~~~~ ~~~~~~~~ ~~~~~~~~~~~~  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  *
 *  2021-07-08 00.02.00 FAE Group     Version 2.0 update                      *
 *                                                                            *
 *                                                                            *
 *****************************************************************************/

/******************************************************************************
**                      		Include Files                                **
******************************************************************************/
#include "system_init.h"


volatile uint16_t PWM_PLUSE_WIDTH =0;//PWM pulse width
volatile uint16_t PWM_cycle=0;	//PWM cycle
volatile uint8_t pwm_DUYT_flag=0;
/* Capture the PWM duty cycle variable */
volatile uint16_t UP_PLUSE_WIDTH = 0;
/*******************************************************************************
**                      	Global Functions 		             	      	  **
*******************************************************************************/
/**
 *  @brief: Delay time
 *  @param in: nms
 *  @param out : None
 *  @retval : None
 */
void delay_ms(volatile uint32_t nms)
{
	volatile uint32_t i, j;
	for (i = 0; i < nms; i++)
	{
		j = 5000;
		while (j--)
			;
	}
}

/**
 *  @brief: GPIOx output initialization configuration
 *  @param in: GPIOx
 * 			   GpioPin
 *  @param out : None
 *  @retval : None
 */
void GPIOInit_Output_Config(GPIO_SFRmap *GPIOx, uint16_t GpioPin)
{
	/* Configure Pxy as output mode parameter */
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_Struct_Init(&GPIO_InitStructure);
	GPIO_InitStructure.m_Pin = GpioPin;
	/* Initialize GPIO output speed */
	GPIO_InitStructure.m_Speed = GPIO_LOW_SPEED;
	/* Initialize GPIO direction as output */
	GPIO_InitStructure.m_Mode = GPIO_MODE_OUT;
	/* Initialize whether the GPIO is pulled up */
	GPIO_InitStructure.m_PullUp = GPIO_NOPULL;
	/* Initialize whether the GPIO is pulled down */
	GPIO_InitStructure.m_PullDown = GPIO_NOPULL;

	GPIO_Configuration(GPIOx, &GPIO_InitStructure);

	/* Initialized to high level */
	GPIO_Set_Output_Data_Bits(GPIOx, GpioPin, Bit_SET);
}

/**
 *  @brief: The CCPx capture function configures the initialization parameters.
 *  @param in: CCPx Pointer to CCP or general timer memory structure
 *  @param out : None
 *  @retval : None
 */
void CCPx_Capture_Mode_init(CCP_SFRmap *CCPx)
{
	/* Set the prescaler value of the timer and the mode of the capture channel */
	/* Timer peripheral reset, enable peripheral clock */
	TIM_Reset(CCPx);
	CCP_PWM_Input_Measurement_Config(CCPx,TRUE); //Set PWM Capture Mode
	/* Set slave mode: reset mode */
	GPTIM_Slave_Mode_Config(CCPx, GPTIM_SLAVE_RESET_MODE);

	/* Select the trigger source : CH1 */
	GPTIM_Trigger_Select_Config(CCPx, GPTIM_TRIGGER_CCPXCH1);
	/* Set the capture channel mode: capture every rising edge */
	CCP_Capture_Mode_Config(CCPx, CCP_CHANNEL_1, CCP_CAP_FALLING_EDGE);
	CCP_Capture_Mode_Config(CCPx, CCP_CHANNEL_2, CCP_CAP_RISING_EDGE);


	/* Update control immediately */
	GPTIM_Updata_Immediately_Config(CCPx, TRUE);
	/* Configuration update enable */
	GPTIM_Updata_Enable(CCPx, TRUE);
	/* Timing mode selection */
	GPTIM_Work_Mode_Config(CCPx, GPTIM_TIMER_MODE);
	/* Timer count value */
	GPTIM_Set_Counter(CCPx, 0);

	/* Timer prescaler value The prescaler is 15+1=16, the HFCLK clock is 16M,
	   1us counts once */
	GPTIM_Set_Prescaler(CCPx, 15);
	/* Up, overflow generates an interrupt flag */
	GPTIM_Counter_Mode_Config(CCPx, GPTIM_COUNT_UP_OF);
	/* Select SCLK as the timer clock source */
	GPTIM_Clock_Config(CCPx, GPTIM_HFCLK);
	/* Enable general timer */
	GPTIM_Cmd(CCPx, TRUE);
	CCP_Channel_INT_Config(CCPx,CCP_CHANNEL_1,TRUE);
	CCP_Clear_Channel_INT_Flag(CCPx,CCP_CHANNEL_1);
	CCP_Channel_INT_Config(CCPx,CCP_CHANNEL_2,TRUE);
	CCP_Clear_Channel_INT_Flag(CCPx,CCP_CHANNEL_2);
	INT_Interrupt_Enable(INT_T18,TRUE);
	INT_Clear_Interrupt_Flag(INT_T18);
	INT_All_Enable(TRUE);

}

/**
 *  @brief :
 *  @param in :None
 *  @param out :None
 *  @retval :None
 */
void UsartGpioInit(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;

	/* Configure PA3 remap mode */
	GPIO_InitStructure.m_Mode = GPIO_MODE_RMP;
	GPIO_InitStructure.m_OpenDrain = GPIO_POD_PP;
	GPIO_InitStructure.m_PullDown = GPIO_NOPULL;
	GPIO_InitStructure.m_PullUp = GPIO_NOPULL;
	GPIO_InitStructure.m_Speed = GPIO_LOW_SPEED;
	GPIO_InitStructure.m_Pin = GPIO_PIN_MASK_1;
	GPIO_Configuration(GPIOE_SFR , &GPIO_InitStructure);
	/* Configure PE0 remap mode, as RX pin, it should be pullup */
	GPIO_InitStructure.m_PullUp = GPIO_PULLUP;
	GPIO_InitStructure.m_Pin = GPIO_PIN_MASK_0;
	GPIO_Configuration(GPIOE_SFR , &GPIO_InitStructure);
	/* Configure PE1 remap function to AF11 */
	GPIO_Pin_RMP_Config(GPIOE_SFR, GPIO_Pin_Num_1, GPIO_RMP_PG1_AF11);
	/* Configure PE0 remap function to AF3 */
	GPIO_Pin_RMP_Config(GPIOE_SFR, GPIO_Pin_Num_0, GPIO_RMP_PG1_AF11);

}

/**
 *  @brief :
 *  @param in :None
 *  @param out :None
 *  @retval :None
 */

void USART_Async_config(USART_SFRmap *USARTx)
{
	USART_InitTypeDef USART_InitStructure;

	USART_Struct_Init(&USART_InitStructure);
    USART_InitStructure.m_Mode=USART_MODE_FULLDUPLEXASY;
    USART_InitStructure.m_TransferDir=USART_DIRECTION_FULL_DUPLEX;
    USART_InitStructure.m_WordLength=USART_WORDLENGTH_8B;
    USART_InitStructure.m_StopBits=USART_STOPBITS_1;
    USART_InitStructure.m_BaudRateBRCKS=USART_CLK_HFCLK;

    /*  Set baudrate to 115200 */
    USART_InitStructure.m_BaudRateInteger=8;
    USART_InitStructure.m_BaudRateNumerator=1;
    USART_InitStructure.m_BaudRateDenominator=13;
	USART_Reset(USARTx);
	USART_Configuration(USARTx,&USART_InitStructure);
    USART_Passageway_Select_Config(USARTx,USART_U7816R_PASSAGEWAY_TX0);
	USART_Clear_Transmit_BUFR_INT_Flag(USARTx);
	USART_RESHD_Enable (USARTx, TRUE);
	USART_Transmit_Data_Enable(USARTx,TRUE);
	USART_Cmd(USARTx,TRUE);
}
/*******************************************************************************
**                     			main Functions 		             	     	  **
*******************************************************************************/
int main()
{
	/* Initialize the system clock is 48M*/
	SystemInit(SYSCLK_FREQ);
	systick_delay_init(SYSCLK_FREQ);
	GPIOInit_Output_Config(GPIOC_SFR, GPIO_PIN_MASK_6);

	UsartGpioInit();
	USART_Async_config(USART1_SFR);

	/* PB7 remap to capture mode */
	GPIO_Write_Mode_Bits(GPIOB_SFR, GPIO_PIN_MASK_7, GPIO_MODE_RMP);
	GPIO_Pin_RMP_Config(GPIOB_SFR, GPIO_Pin_Num_7, GPIO_RMP_PG2_AF0);

	/* CCP18 channel 1 and channel 2 Capture mode initialization, Configure 
	   channel 1 trigger source*/
	CCPx_Capture_Mode_init(CCP18_SFR);

	fprintf(USART1_STREAM,"power on!\r\n");
	while (1)
	{


		/* Capture duty cycle */

		if(1== pwm_DUYT_flag)
		{
			fprintf(USART1_STREAM, "PWM Duty:%d\r\n",PWM_PLUSE_WIDTH);

			fprintf(USART1_STREAM, "PWM T:%d\r\n",PWM_cycle);

			pwm_DUYT_flag=0;
			GPIO_Toggle_Output_Data_Config(GPIOC_SFR, GPIO_PIN_MASK_6);
		}


	}
}

/**
 *  @brief : Reports the name of the source file and the source line number
 *           where the assert_param error has occurred.
 *  @param in :	file pointer to the source file name
 * 	@param in :	line assert_param error line source number
 *  @param out :None
 *  @retval :None
 */
void check_failed(uint8_t *File, uint32_t Line)
{
	/* User can add his own implementation to report the file name and line number,
		ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

	/* Infinite loop */
	while (1)
	{
		;
	}
};
